 package com.nondo.dean.platform.tool;

 import java.security.AlgorithmParameters;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.Security;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

import org.bouncycastle.jce.provider.BouncyCastleProvider;

 public class HmacUtil {
     
     public static boolean initialized = false;

     public static String SHA1(String str){  
         try {  
             //指定sha1算法  
             MessageDigest digest = MessageDigest.getInstance("SHA-1");  
             digest.update(str.getBytes());  
             //获取字节数组  
             byte messageDigest[] = digest.digest();  
             // Create Hex String  
             StringBuffer hexString = new StringBuffer();  
             // 字节数组转换为 十六进制 数  
             for (int i = 0; i < messageDigest.length; i++) {  
                 String shaHex = Integer.toHexString(messageDigest[i] & 0xFF);  
                 if (shaHex.length() < 2) {  
                     hexString.append(0);  
                 }  
                 hexString.append(shaHex);  
             }  
             return hexString.toString().toLowerCase();  
   
         } catch (NoSuchAlgorithmException e) {  
            return "";
         }  
     } 
     
     public static byte[] decrypt(byte[] content, byte[] keyByte, byte[] ivByte) throws InvalidAlgorithmParameterException {
         initialize();
         try {
             Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
             Key sKeySpec = new SecretKeySpec(keyByte, "AES");

             cipher.init(Cipher.DECRYPT_MODE, sKeySpec, generateIV(ivByte));// 初始化
             byte[] result = cipher.doFinal(content);
             return result;
         } catch (NoSuchAlgorithmException e) {
             e.printStackTrace();
         } catch (NoSuchPaddingException e) {
             e.printStackTrace();
         } catch (InvalidKeyException e) {
             e.printStackTrace();
         } catch (IllegalBlockSizeException e) {
             e.printStackTrace();
         } catch (BadPaddingException e) {
             e.printStackTrace();
         } catch (NoSuchProviderException e) {
             // TODO Auto-generated catch block
             e.printStackTrace();
         } catch (Exception e) {
             // TODO Auto-generated catch block
             e.printStackTrace();
         }
         return null;
     }

     public static void initialize() {
         if (initialized)
             return;
         Security.addProvider(new BouncyCastleProvider());
         initialized = true;
     }

     // 生成iv
     public static AlgorithmParameters generateIV(byte[] iv) throws Exception {
         AlgorithmParameters params = AlgorithmParameters.getInstance("AES");
         params.init(new IvParameterSpec(iv));
         return params;
     }
 }